﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Services;
using System.Web.Script.Services;
using System.ComponentModel;
using umbraco.BasePages;
using Procure.Data.Context;

namespace Procure.Web.webServices
{
    /// <summary>
    /// Summary description for procureAjaxService
    /// </summary>
    [WebService(Namespace = "http://procure.codeplex.com/")]
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
    [ToolboxItem(false)]
    [ScriptService]


    public class procureAjaxService : System.Web.Services.WebService
    {

        [WebMethod]
        [ScriptMethod]
        public string GetProductName(int productId)
        {

            Authorize();

            ProcureDataContext context = DataFactory.GetDataContext();
            Product product = context.Products.FirstOrDefault(p => p.Id == productId);
            string productName = product != null ? product.Name : "unknown";

            return productName;
        }

        public static void Authorize()
        {
            if (!BasePage.ValidateUserContextID(BasePage.umbracoUserContextID))
                throw new Exception("Client authorization failed. User is not logged in");

        }
    }
}
